﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.IO;
using SPThumbnailExtender.Interne;
using System.Text;
using System.Web;

namespace SPThumbnailExtender.Layouts.SPThumbnailExtender
{
    public partial class ThumbSilverlight : UnsecuredLayoutsPageBase
    {
        public string strUrlSilverlight;
        public string strUrlDoc;
        public string strWebUrl;
        protected override bool AllowAnonymousAccess
        {
            get
            {
                return true;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string docUrl=Server.UrlDecode(Request.QueryString["doc"]);

            string ItemID = Request.QueryString["id"];
            int iID;
            int.TryParse(ItemID,out iID);
            string ListSource = Request.QueryString["source"];

            StringBuilder strBuild = new StringBuilder();
            if (ListSource != string.Empty && iID > 0)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID,SPContext.Current.Site.Zone))
                    {
                        using (SPWeb web =site.OpenWeb(SPContext.Current.Web.ID))
                        {
                            Guid listId = new Guid(ListSource);
                            SPList lst = web.Lists[listId];
                             
                            SPListItem li = lst.Items.GetItemById(iID);
                            ThumbnailBase thBase = ThumbnailFactory.GenerateThumbnail(new Uri(web.Site.Url + li.File.ServerRelativeUrl, UriKind.Absolute));
                            int iCount = thBase.GetCount();
                            for (int miind = 0; miind < iCount; miind++)
                            {
                                string strDocUrl = HttpUtility.UrlEncode(li.File.ServerRelativeUrl);
                                strBuild.Append("<a  href=\"" + web.Url + "/_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?w=640&h=480&usecache=true&doc=" + strDocUrl + "&n=" + miind + "\" >\n");
                                strBuild.Append("<img  src=\"" + web.Url + "/_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?w=150&h=75&usecache=true&doc=" + strDocUrl + "&n=" + miind + "\" />\n");
                                strBuild.Append("</a>\n");

                            }
                        }
                    }
                });
            }
            else
            {

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(docUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPListItem li = web.GetListItem(docUrl);

                            ThumbnailBase thBase = ThumbnailFactory.GenerateThumbnail(new Uri(site.Url + li.File.ServerRelativeUrl, UriKind.Absolute));
                            int iCount = thBase.GetCount();
                            string strDocUrl = HttpUtility.UrlEncode(li.File.ServerRelativeUrl);

                            for (int miind = 0; miind < iCount; miind++)
                            {
                                strBuild.Append("<a  href=\"" + web.Url + "/_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?w=640&h=480&usecache=true&doc=" + strDocUrl + "&n=" + miind + "\" >\n");
                                strBuild.Append("<img  src=\"" + web.Url + "/_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?w=150&h=75&usecache=true&doc=" + strDocUrl + "&n=" + miind + "\" />\n");
                                strBuild.Append("</a>\n");

                            }

                        }

                    }
                });
            }
            LtImage.Text = strBuild.ToString();
        }
    }
}
